package diploma.kisel.modelas.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Domain class for Computer (table - pc).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "pc")
@NamedQueries({
		@NamedQuery(name = "Computer.findAll", query = "select c from Computer c"),
		@NamedQuery(name = "Computer.findByPrice", query = "select c from Computer c where c.cost >= :minPrice and c.cost <= :maxPrice"),
		@NamedQuery(name = "Computer.findById", query = "select c from Computer c where c.id = :id"),
		@NamedQuery(name = "Computer.findByType", query = "select c from Computer c where c.type = :type"),
		@NamedQuery(name = "Computer.findByPriceAndType", query = "select c from Computer c where c.cost >= :minPrice and c.cost <= :maxPrice and c.type = :type")})
public class Computer {

	private Integer id;
	private String number;
	private ComputerType type;
	private String brand;
	private Integer cost;
	private String features;

	// for many to many mapping
	private Set<Workstation> workstations = new HashSet<Workstation>();

	public Computer() {

	}

	public Computer(String number, ComputerType type, String brand, Integer cost) {
		this.number = number;
		this.type = type;
		this.brand = brand;
		this.cost = cost;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_pc")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "number")
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	public ComputerType getType() {
		return type;
	}

	public void setType(ComputerType type) {
		this.type = type;
	}

	@Column(name = "brand")
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	@Column(name = "cost")
	public Integer getCost() {
		return cost;
	}

	public void setCost(Integer cost) {
		this.cost = cost;
	}

	@Column(name = "features")
	public String getFeatures() {
		return features;
	}

	public void setFeatures(String features) {
		this.features = features;
	}

	@ManyToMany
	@JoinTable(name = "list_pc", joinColumns = @JoinColumn(name = "id_pc"), inverseJoinColumns = @JoinColumn(name = "id_workstation"))
	public Set<Workstation> getWorkstations() {
		return workstations;
	}

	public void setWorkstations(Set<Workstation> workstations) {
		this.workstations = workstations;
	}	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Computer other = (Computer) obj;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Computer [id=" + id + ", number=" + number + ", type=" + type
				+ ", brand=" + brand + ", cost=" + cost + ", features="
				+ features + "]";
	}

}
